package com.ruoyi.web.controller.monitor;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.InterfaceLog;
import com.ruoyi.system.service.IInterfaceLogService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 接口日志Controller
 *
 * @author ruoyi
 * @date 2023-02-13
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/monitor/interfaceLog" )
public class InterfaceLogController extends BaseController {

    private final IInterfaceLogService iInterfaceLogService;

    /**
     * 查询接口日志列表
     */
    @GetMapping("/list")
    public TableDataInfo list(InterfaceLog interfaceLog)
    {
        startPage();
        LambdaQueryWrapper<InterfaceLog> lqw = new LambdaQueryWrapper<InterfaceLog>();
        if (StringUtils.isNotBlank(interfaceLog.getSerialNumber())){
            lqw.eq(InterfaceLog::getSerialNumber ,interfaceLog.getSerialNumber());
        }
        if (interfaceLog.getStartTime() != null){
            lqw.eq(InterfaceLog::getStartTime ,interfaceLog.getStartTime());
        }
        if (interfaceLog.getEndTime() != null){
            lqw.eq(InterfaceLog::getEndTime ,interfaceLog.getEndTime());
        }
        if (StringUtils.isNotBlank(interfaceLog.getInterfaceDirection())){
            lqw.eq(InterfaceLog::getInterfaceDirection ,interfaceLog.getInterfaceDirection());
        }
        if (StringUtils.isNotBlank(interfaceLog.getPacketSize())){
            lqw.eq(InterfaceLog::getPacketSize ,interfaceLog.getPacketSize());
        }
        if (StringUtils.isNotBlank(interfaceLog.getInterfaceType())){
            lqw.eq(InterfaceLog::getInterfaceType ,interfaceLog.getInterfaceType());
        }
        if (StringUtils.isNotBlank(interfaceLog.getInterfaceResponse())){
            lqw.eq(InterfaceLog::getInterfaceResponse ,interfaceLog.getInterfaceResponse());
        }
        if (StringUtils.isNotBlank(interfaceLog.getTimeConsuming())){
            lqw.eq(InterfaceLog::getTimeConsuming ,interfaceLog.getTimeConsuming());
        }
        List<InterfaceLog> list = iInterfaceLogService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出接口日志列表
     */
    @Log(title = "接口日志" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(InterfaceLog interfaceLog) {
        LambdaQueryWrapper<InterfaceLog> lqw = new LambdaQueryWrapper<InterfaceLog>(interfaceLog);
        List<InterfaceLog> list = iInterfaceLogService.list(lqw);
        ExcelUtil<InterfaceLog> util = new ExcelUtil<InterfaceLog>(InterfaceLog. class);
        return util.exportExcel(list, "log" );
    }

    /**
     * 获取接口日志详细信息
     */
    @GetMapping(value = "/{logId}" )
    public AjaxResult getInfo(@PathVariable("logId" ) Long logId) {
        return AjaxResult.success(iInterfaceLogService.getById(logId));
    }

    /**
     * 新增接口日志
     */
    @Log(title = "接口日志" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InterfaceLog interfaceLog) {
        return toAjax(iInterfaceLogService.save(interfaceLog) ? 1 : 0);
    }

    /**
     * 修改接口日志
     */
    @Log(title = "接口日志" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InterfaceLog interfaceLog) {
        return toAjax(iInterfaceLogService.updateById(interfaceLog) ? 1 : 0);
    }

    /**
     * 删除接口日志
     */
    @Log(title = "接口日志" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{logIds}" )
    public AjaxResult remove(@PathVariable Long[] logIds) {
        return toAjax(iInterfaceLogService.removeByIds(Arrays.asList(logIds)) ? 1 : 0);
    }
}
